package com.zjx.reggie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjx.reggie.entity.*;
import com.zjx.reggie.mapper.OrdersMapper;
import com.zjx.reggie.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    AddressBookService addressBookService;
    //phone address address_book_id

    @Autowired
    UserService userService;
    //userId  userName

    @Autowired
    ShoppingCartService shoppingCartService;

    @Autowired
    OrderDetailService orderDetailService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addOrder(String userId, Orders orders) {
//        A. 获得当前用户id, 查询当前用户的购物车数据
            LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,userId);
            List<ShoppingCart> shoppingList = shoppingCartService.list(shoppingCartLambdaQueryWrapper);

//            if(shoppingList == null || shoppingList.size() == 0){
//                throw new CustomException("购物车为空，不能下单");
//            }

            double amount = 0;
            for(int i=0;i<shoppingList.size();i++){
                amount += shoppingList.get(i).getNumber()*shoppingList.get(i).getAmount();
            }


//        B. 根据当前登录用户id, 查询用户数据
            User user = userService.getById(userId);

//        C. 根据地址ID, 查询地址数据
            LambdaQueryWrapper<AddressBook> addressBookLambdaQueryWrapper = new LambdaQueryWrapper<>();
            addressBookLambdaQueryWrapper.eq(AddressBook::getUserId,userId);
            addressBookLambdaQueryWrapper.eq(AddressBook::getIsDefault,1);
            AddressBook addressBook = addressBookService.getOne(addressBookLambdaQueryWrapper);
//            if(addressBook==null){
//                throw new CustomException("用户地址信息有误，不能下单");
//            }
            //E. 组装订单数据, 保存订单数据
            orders.setUserName(user.getName());
            orders.setAmount(amount);
            orders.setAddressBookId(addressBook.getId());
            orders.setPhone(addressBook.getPhone());
            orders.setAddress(addressBook.getDetail());
            orders.setConsignee(addressBook.getConsignee());

            orders.setStatus(1);
            orders.setNumber(UUID.randomUUID().toString());
            orders.setUserId(userId);
            orders.setOrderTime(new Date());
            orders.setCheckoutTime(new Date());
            save(orders);

//        D. 组装订单明细数据, 批量保存订单明细
            List<OrderDetail> orderDetailList = new ArrayList<>();
            for(int i=0;i<shoppingList.size();i++){
                OrderDetail orderDetail = new OrderDetail();
                orderDetail.setName(shoppingList.get(i).getName());
                orderDetail.setImage(shoppingList.get(i).getImage());
                orderDetail.setOrderId(Long.valueOf(orders.getId()));
                orderDetail.setDishId(Long.valueOf(shoppingList.get(i).getDishId()));
                if(shoppingList.get(i).getSetmealId()!=null){
                    orderDetail.setSetmealId(Long.valueOf(shoppingList.get(i).getSetmealId()));
                }
                orderDetail.setDishFlavor(shoppingList.get(i).getDishFlavor());
                orderDetail.setNumber(shoppingList.get(i).getNumber());
                orderDetail.setAmount(shoppingList.get(i).getAmount()*shoppingList.get(i).getNumber());
                orderDetailList.add(orderDetail);
            }
            orderDetailService.saveBatch(orderDetailList);

//        F. 删除当前用户的购物车列表数据
            shoppingCartService.remove(shoppingCartLambdaQueryWrapper);
            return true;
    }
}
